package com.kkm.service.Imp;

import com.kkm.dao.Imp.SongListDaoImp;
import com.kkm.pojo.Order;
import com.kkm.pojo.PageBean;
import com.kkm.pojo.Song;
import com.kkm.pojo.SongList;
import com.kkm.service.SongListService;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class SongListServiceImp implements SongListService {
    SongListDaoImp songListDaoImp= new SongListDaoImp();
    @Override
    public PageBean<SongList> selectAll(String currentPage, int rows) {
        int currPage=Integer.parseInt(currentPage);
        if(currPage<=0){
            currPage=1;
        }
        PageBean<SongList> pageBean=new PageBean(); //实例化pagebean对象
        int count= songListDaoImp.checkSongListCount();//查询记录数

        int totalPage=count%rows==0?count/rows :count/rows+1;
        if(currPage>=totalPage){
            currPage=totalPage;
            System.out.println(currPage+"OrderService");
        }

        int index=(currPage-1)*rows;
        List<SongList> songList=songListDaoImp.showAll(index,rows);

        pageBean.setRows(rows);
        pageBean.setCurrentPage(currPage);
        pageBean.setTotalPage(totalPage);
        pageBean.setTotalCount(count);
        pageBean.setList(songList);

        return pageBean;
    }

    @Override
    public PageBean<SongList> checkSongList(Map<String, String[]> condition, String currentPage, int rows) {
        int orderCount=songListDaoImp.checkSongListCount();
        int num=0;
        Set<String> keySet=condition.keySet();
        for(String key:keySet) {
            String value = condition.get(key)[0];//获取值
            if (!"".equals(value)) {
                num++;
            }
        }
        if(num>2){
            orderCount=songListDaoImp.checkSongListNum(condition);
        }

        if(orderCount>=1){
            int currPage=Integer.parseInt(currentPage);
            PageBean<SongList> pageBean=new PageBean(); //实例化pagebean对象

            int totalPage=orderCount%rows==0?orderCount/rows :orderCount/rows+1;
            int index=(currPage-1)*rows;
            List<SongList> songlist=songListDaoImp.checkSongList(condition,index,rows);

            pageBean.setRows(rows);
            pageBean.setCurrentPage(currPage);
            pageBean.setTotalPage(totalPage);
            pageBean.setTotalCount(orderCount);
            pageBean.setList(songlist);
            return pageBean;
        }
        return null;
    }

    @Override
    public void deleteSongList(int sid) {
        songListDaoImp.deleteSongList(sid);
    }


    @Override
    public void addSongList(String title, String pic, String introduction, String style) {

        songListDaoImp.addSongList(title,pic,introduction,style);
    }

    @Override
    public PageBean<Song> showAllMusic(String slid, String currentPage, int rows) {
        int currPage=Integer.parseInt(currentPage);
        if(currPage<=0){
            currPage=1;
        }

        int count=songListDaoImp.checkMusicListCount(slid);        //查询记录数
        if(count>=1){
            PageBean<Song> pageBean=new PageBean(); //实例化pagebean对象
            int totalPage=count%rows==0?count/rows :count/rows+1;
            if(currPage>=totalPage){
                currPage=totalPage;
                System.out.println(currPage+"UserService");
            }

            int index=(currPage-1)*rows;
            List<Song> musics=songListDaoImp.showMusicListSong(slid,index,rows);

            pageBean.setRows(rows);
            pageBean.setCurrentPage(currPage);
            pageBean.setTotalPage(totalPage);
            pageBean.setTotalCount(count);
            pageBean.setList(musics);

            return pageBean;
        }
        return null;
    }

}
